package com.pipi.sso.core.service.impl;

import com.pipi.sso.core.exception.DormServiceException;
import com.pipi.sso.core.service.RepairService;
import com.pipi.sso.core.enums.CommonStatusEnum;
import com.pipi.sso.core.enums.DormErrorCodeEnum;
import com.pipi.sso.core.enums.DormEnums;
import com.pipi.sso.core.model.RepairBO;
import com.pipi.sso.core.util.BeanUtils;
import com.pipi.sso.core.util.DateUtil;
import com.pipi.sso.core.util.PriceUtil;
import com.pipi.sso.dao.manager.RepairManager;
import com.pipi.sso.dao.model.RepairDO;
import com.pipi.sso.dao.query.RepairQuery;
import com.pipi.sso.dao.support.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * All rights Reserved, Designed By www.maihaoche.com
 *
 * @Package com.pipi.sso.core.service.impl
 * @date: 2019/4/15 4:14 PM
 * @Copyright: 2017-2020 www.maihaoche.com Inc. All rights reserved.
 */
@Service
@Slf4j
public class RepairServiceImpl implements RepairService {

    @Resource
    private RepairManager repairManager;

    @Override
    public PageResult<RepairBO> queryByPaged(RepairBO repairBO) {
        RepairQuery repairQuery = buildQuery(repairBO);
        repairQuery.setPageNo(repairBO.getPage());
        repairQuery.setPageSize(repairBO.getLimit());
        try {
            PageResult<RepairDO> repairDOPageResult = repairManager.selectByQueryWithPage(repairQuery);
            List<RepairBO> repairBOS = buildBOs(repairDOPageResult.getResult());
            return PageResult.createPageResult(repairBOS, repairDOPageResult.getPageInfo());
        } catch (Exception e) {
            log.error("查询报修失败!", e);
            throw new DormServiceException(DormErrorCodeEnum.EXCEPTION.getErrCode(), "查询报修失败");
        }
    }

    @Override
    public List<RepairBO> queryNoPaged(RepairBO repairBO) {
        RepairQuery repairQuery = buildQuery(repairBO);
        try {
            List<RepairDO> repairDOS = repairManager.selectByQuery(repairQuery);
            return buildBOs(repairDOS);
        } catch (Exception e) {
            log.error("查询报修失败!", e);
            throw new DormServiceException(DormErrorCodeEnum.EXCEPTION.getErrCode(), "查询报修失败");
        }
    }

    @Override
    public void addRepair(RepairBO repairBO) {
        try {
            RepairDO repairDO = BeanUtils.createCloneBean(repairBO, RepairDO.class);
            repairDO.setStatus(DormEnums.RepairStatusEnum.PENDING.getCode());
            repairDO.setIsDeleted(CommonStatusEnum.NO.getCode());
            repairManager.insertSelective(repairDO);
        } catch (Exception e) {
            log.error("添加报修失败！", e);
            throw new DormServiceException(DormErrorCodeEnum.EXCEPTION.getErrCode(), "添加报修失败");
        }
    }

    @Override
    public void deleteRepair(List<Long> repairIds) {
        try {
            RepairQuery repairQuery = new RepairQuery();
            repairQuery.createCriteria().andRepairIdIn(repairIds);

            RepairDO repairDO = new RepairDO();
            repairDO.setIsDeleted(CommonStatusEnum.YES.getCode());
            repairManager.updateByQuerySelective(repairDO, repairQuery);
        } catch (Exception e) {
            log.error("删除报销失败！");
            throw new DormServiceException(DormErrorCodeEnum.EXCEPTION.getErrCode(), "取消失败");
        }
    }

    @Override
    public void updateRepair(RepairBO repairBO) {
        try {
            RepairDO repairDO = BeanUtils.createCloneBean(repairBO, RepairDO.class);
            repairDO.setRepairAmount(PriceUtil.convertPriceYuan(repairBO.getRepairAmountDesc()));
            if (StringUtils.isNotBlank(repairBO.getHandler())) {
                repairDO.setStatus(DormEnums.RepairStatusEnum.processed.getCode());
            }
            repairManager.updateByPrimaryKeySelective(repairDO);
        } catch (Exception e) {
            log.error("修改报销失败", e);
            throw new DormServiceException(DormErrorCodeEnum.EXCEPTION);
        }
    }

    private RepairQuery buildQuery(RepairBO repairBO) {
        RepairQuery query = new RepairQuery();
        query.setOrderByClause("gmt_create desc");
        RepairQuery.Criteria criteria = query.createCriteria().andIsDeletedEqualTo(CommonStatusEnum.NO.getCode());
        if (Objects.nonNull(repairBO)) {
            if (Objects.nonNull(repairBO.getRepairId())) {
                criteria.andRepairIdEqualTo(repairBO.getRepairId());
            }
            if (StringUtils.isNotBlank(repairBO.getRoomNo())) {
                criteria.andRoomNoLike("%" + repairBO.getRoomNo() + "%");
            }
            if (StringUtils.isNotBlank(repairBO.getAssetsName())) {
                criteria.andAssetsNameLike("%" + repairBO.getAssetsName() + "%");
            }
            if (Objects.nonNull(repairBO.getStatus())) {
                criteria.andStatusEqualTo(repairBO.getStatus());
            }
            if (Objects.nonNull(repairBO.getUserId())) {
                criteria.andUserIdEqualTo(repairBO.getUserId());
            }
        }
        return query;
    }

    private List<RepairBO> buildBOs(List<RepairDO> repairDOS) {
        return repairDOS.stream().map(this::buildBO).collect(Collectors.toList());
    }


    private RepairBO buildBO(RepairDO repairDO) {
        if (Objects.isNull(repairDO)) {
            return null;
        }
        RepairBO repairBO = BeanUtils.createCloneBean(repairDO, RepairBO.class);
        repairBO.setStatusDesc(DormEnums.RepairStatusEnum.getDescByCode(repairBO.getStatus()));
        repairBO.setGmtCreateDesc(DateFormatUtils.format(repairBO.getGmtCreate(), DateUtil.PATTERN_1));
        repairBO.setRepairAmountDesc(PriceUtil.formatNoGroup(repairBO.getRepairAmount()));
        return repairBO;
    }
}
